@use '@material/ripple/mixins' as mdc-ripple;
@use '@material/textfield' as mdc-textfield;
@use '@material/floating-label' as mdc-floating-label;
@use '@material/notched-outline' as mdc-notched-outline;
@use '@material/line-ripple' as mdc-line-ripple;
@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/typography' as mdc-typography;
@use '../mdc-helpers/mdc-helpers';
@use 'form-field-density';
@use 'form-field-subscript';
@use 'form-field-focus-overlay';
@use 'form-field-native-select';
@use 'mdc-text-field-theme-variable-refresh';
@use '../../material/core/theming/theming';


// Mixin that overwrites the default MDC text-field color styles to be based on
// the given theme palette. The MDC text-field is styled using `primary` by default.
@mixin _mat-mdc-text-field-color-styles($palette-name,
  $query: mdc-helpers.$mat-theme-styles-query) {
  $_mdc-text-field-focused-label-color: mdc-textfield.$focused-label-color;
  mdc-textfield.$focused-label-color: rgba(mdc-theme-color.prop-value($palette-name), 0.87);

  @include mdc-textfield.caret-color($palette-name, $query);
  @include mdc-textfield.line-ripple-color($palette-name, $query);

  .mdc-text-field--focused {
    @include mdc-textfield.focused_($query);
  }

  .mdc-text-field--invalid {
    @include mdc-textfield.invalid_($query);
  }

  .mdc-text-field--outlined {
    @include mdc-textfield.focused-outline-color($palette-name, $query);
  }

  mdc-textfield.$focused-label-color: $_mdc-text-field-focused-label-color;
}

@mixin color($config-or-theme) {
  $config: theming.get-color-config($config-or-theme);
  @include mdc-helpers.mat-using-mdc-theme($config) {
    @include mdc-text-field-theme-variable-refresh.private-text-field-refresh-theme-variables() {
      @include mdc-textfield.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
      @include mdc-floating-label.core-styles($query: mdc-helpers.$mat-theme-styles-query);
      @include mdc-notched-outline.core-styles($query: mdc-helpers.$mat-theme-styles-query);
      @include mdc-line-ripple.core-styles($query: mdc-helpers.$mat-theme-styles-query);
      @include form-field-subscript.private-form-field-subscript-color();
      @include form-field-focus-overlay.private-form-field-focus-overlay-color();
      @include form-field-native-select.private-form-field-native-select-color($config);

      .mat-mdc-form-field.mat-accent {
        @include _mat-mdc-text-field-color-styles(secondary);
      }

      .mat-mdc-form-field.mat-warn {
        @include _mat-mdc-text-field-color-styles(error);
      }
    }
  }
}

@mixin typography($config-or-theme) {
  $config: theming.get-typography-config($config-or-theme);
  @include mdc-helpers.mat-using-mdc-typography($config) {
    @include mdc-textfield.without-ripple($query: mdc-helpers.$mat-typography-styles-query);
    @include mdc-floating-label.core-styles($query: mdc-helpers.$mat-typography-styles-query);
    @include mdc-notched-outline.core-styles($query: mdc-helpers.$mat-typography-styles-query);
    @include mdc-line-ripple.core-styles($query: mdc-helpers.$mat-typography-styles-query);
    @include form-field-subscript.private-form-field-subscript-typography($config);

    // MDC uses the `subtitle1` level for the input label and value, but the spec shows `body1` as
    // the correct level.
    .mat-mdc-input-element,
    .mat-mdc-form-field label,
    .mat-mdc-form-field-text-prefix,
    .mat-mdc-form-field-text-suffix {
      @include mdc-typography.typography(body1, $query: mdc-helpers.$mat-typography-styles-query);
    }
  }
}

@mixin density($config-or-theme) {
  $density-scale: theming.get-density-config($config-or-theme);
  @include form-field-density.private-form-field-density($density-scale);
}

@mixin theme($theme-or-color-config) {
  $theme: theming.private-legacy-get-theme($theme-or-color-config);
  @include theming.private-check-duplicate-theme-styles($theme, 'mat-mdc-form-field') {
    $color: theming.get-color-config($theme);
    $density: theming.get-density-config($theme);
    $typography: theming.get-typography-config($theme);

    @if $color != null {
      @include color($color);
    }
    @if $density != null {
      @include density($density);
    }
    @if $typography != null {
      @include typography($typography);
    }
  }
}
